@page "/Ingress"
@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using Extension
@using k8s.Models
@inherits BlazorApp.Pages.Common.TableBase<V1Ingress>
@if (TableData.PagedItems != null)
{
    <HTable Class="wideTable">
        <HTr>
            <HTd >
                <SearchToolBar Title=@L["Ingress"] Count="@TableData.Total"
                               OnItemDeletedCallback="@(async () => await InvokeAsync(StateHasChanged))"
                               OnSelectedItemCloseClicked="@TableData.RemoveSelection"
                               OnRemoveAllClicked="@TableData.RemoveAllSelection" TableData="@TableData"
                               OnSearch="OnSearchHandler" TItem="V1Ingress"
                               OnNsSelected="OnNsSelectedHandler">
                </SearchToolBar>
            </HTd>
        </HTr>
        <HTr>
            <HTd >
                <Table TItem="V1Ingress"
                       DataSource="@TableData.PagedItems"
                       Size=@TableSize.Small Bordered="true"
                       Total="TableData.Total"
                       @bind-PageIndex="TableData.PageIndex"
                       @bind-PageSize="TableData.PageSize"
                       @bind-SelectedRows="TableData.SelectedRows"
                       Loading="TableData.Loading"
                       OnChange="OnPageChangeHandler">
                    <Selection Key="@(context.Name())"/>
                    <AntDesign.Column Field="@context.Name()" Title=@L["Name"]>
                        <Button OnClick="@(() => OnItemNameClick(context))" Type="@ButtonType.Link">@context.Name()</Button>
                    </AntDesign.Column>
                    <AntDesign.Column Field="@context.Namespace()" Title=@L["Namespace"] Width="110px"/>

                    <AntDesign.Column Title=@L["IngressClass"] TData="string">
                        @context.Spec.IngressClassName
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Detail"] TData="string">
                        @if (context.Spec.Rules is { Count: > 0 })
                        {
                            @foreach (var rule in context.Spec.Rules)
                            {
                                @foreach (var path in rule.Http.Paths)
                                {
                                    <div>
                                        <a href="@IngressService.GetRulePathDisplayUrl(rule, path, context.Spec.Tls)"
                                           target="_blank">
                                            @IngressService.GetRulePathDisplayUrl(rule, path, context.Spec.Tls)
                                        </a>
                                        @{
                                            var backend = IngressService.GetRulePathBackend(path);
                                        }
                                        @if (!backend.IsNullOrEmpty())
                                        {
                                            @(" -> ")
                                            <RefView Ref="@IngressService.GetRulePathBackend(path, context.Namespace())"
                                                     FullView="true">
                                            </RefView>
                                            <Tag>@path?.Backend?.Service?.Port?.Number</Tag>
                                        }
                                    </div>
                                }
                            }
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Age"] TData="string" Width="100px">
                        <AgeView Age="@context.CreationTimestamp()"></AgeView>
                    </AntDesign.Column>
                    <ActionColumn Title=@L["Action"] Width="100px">
                        <IngressAction Item="@context"></IngressAction>
                    </ActionColumn>
                </Table>
            </HTd>
        </HTr>
    </HTable>

    <ResourceWatcher OnResourceChanged="OnResourceChanged" T="V1Ingress"></ResourceWatcher>
}
